
import 'package:common_utils/common_utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:nft_shop/market/conteroller/market_details_conteroller.dart';
import 'package:nft_shop/res/global.dart';
import 'package:nft_shop/widgets/load_image.dart';

enum WorksInfoType {
  info,
  framer,
  describe,
}

class WorksInfoWidget extends StatefulWidget {
  WorksInfoType type;

  WorksInfoWidget({Key? key,required this.type}) : super(key: key);

  @override
  _WorksInfoWidgetState createState() => _WorksInfoWidgetState();
}

class _WorksInfoWidgetState extends State<WorksInfoWidget> {

  late MarketDetailsConteroller _conteroller;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _conteroller = Get.find<MarketDetailsConteroller>();
  }

  @override
  Widget build(BuildContext context) {
    if(widget.type == WorksInfoType.info){
      return _basicInfoWidget();
    }else if(widget.type == WorksInfoType.framer){
      return _framerWidget();
    }else{
      return _worksDescribeWidget();
    }
  }


  ///基本信息
  Widget _basicInfoWidget(){
    return Column(
      children: [
        _infoTextItem("所属专辑",_conteroller.goodsinfo.info!.specialAlbumName??""),
        _infoTextItem("编      号","#${_conteroller.goodsinfo.info!.code??""}/${_conteroller.goodsinfo.info!.allCount??""}",isArrows: false),
        _infoTextAddressItem("合约地址",_conteroller.goodsinfo.info!.contractAddress??""),
        _infoTextItem("链上标识",_conteroller.goodsinfo.info!.chainMark??""),
        _infoTextItem("发布时间",_conteroller.goodsinfo.info!.createTime??""),
      ],
    );
  }

  ///基本信息Item
  Widget _infoTextItem(String title,String content,{bool isArrows = false}){
    return Container(
      padding: EdgeInsets.only(top: 7),
      child: Row(
        children: [
          Text(title,style: TextStyle(fontSize: 12,color: Global.kTheme.inputHint1,fontFamily: Global.mySemiboldFont),),
          SizedBox(width: 15,),
          Expanded(
            child: Row(
              children: [
                Text(content,style: TextStyle(fontSize: 12,color: Global.kTheme.textPrimary1,fontFamily: Global.mySemiboldFont,),),
                isArrows?Spacer():Container(),
                isArrows?Text("查看其它编号",style: TextStyle(fontSize: 12,color: Global.kTheme.textPrimary3,fontFamily: Global.mySemiboldFont),):Container(),
                isArrows?SizedBox(width: 3,):Container(),
                Visibility(visible: isArrows, child: LoadImage("icon_arrows",width: 12,height: 12,),),
              ],
            ),
          )

        ],
      ),
    );
  }
  ///基本信息Item 合约地址
  Widget _infoTextAddressItem(String title,String content,){
    return Container(
      padding: EdgeInsets.only(top: 7),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.start,
        children: [
          Text(title,style: TextStyle(fontSize: 12,color: Global.kTheme.inputHint1,fontFamily: Global.mySemiboldFont),),
          SizedBox(width: 15,),
         Text(omitAddress(content),style: TextStyle(fontSize: 12,color: Global.kTheme.textPrimary1,fontFamily: Global.mySemiboldFont,),),
        ],
      ),
    );
  }

  ///创作者介绍
  Widget _framerWidget(){
    return Container(
      padding: EdgeInsets.only(top: 7),
      child: Column(
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.start,
            children: [
              ClipRRect(borderRadius: BorderRadius.circular(21),child: LoadImage(_conteroller.goodsinfo.info!.portrait??"",width: 42,height: 42,)),
              SizedBox(width: 5,),
              Expanded(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(_conteroller.goodsinfo.info!.nickname??"",maxLines: 1,overflow: TextOverflow.ellipsis, style: TextStyle(fontSize: 14,color: Global.kTheme.textPrimary0,fontFamily: Global.mySemiboldFont),),
                    InkWell(
                      onTap: (){
                        ClipboardData data = new ClipboardData(
                            text: _conteroller.goodsinfo.info!.framerAccountAddress??"");
                        Clipboard.setData(data);
                        EasyLoading.showToast("复制成功");
                      },
                      child: Row(
                        children: [
                          Text(omitAddress(_conteroller.goodsinfo.info!.framerAccountAddress??""),maxLines: 1,style: TextStyle(fontSize: 12,color: Global.kTheme.inputHint1,fontFamily: Global.mySemiboldFont,overflow: TextOverflow.ellipsis),),
                          SizedBox(width: 4,),
                          LoadImage("my_copy",width: 16,height: 16,),
                        ],
                      ),
                    )
                  ],
                ),
              )
            ],
          ),
          SizedBox(height: 10,),
          Text(_conteroller.goodsinfo.info!.introduce??"",
          style: TextStyle(fontSize: 12,color: Global.kTheme.inputHint1,fontFamily: Global.mySemiboldFont),
          )
        ],
      ),
    );
  }

  ///作品描述
  Widget _worksDescribeWidget(){
    return Container(
      padding: EdgeInsets.only(top: 7),
      child: Text(_conteroller.goodsinfo.info!.detail??"",
        style: TextStyle(fontSize: 12,color: Global.kTheme.inputHint1,fontFamily: Global.mySemiboldFont),
      ),
    );
  }

  String omitAddress(String address, {int len = 4}) {
    if (address == null || address.isEmpty) return "";
    if (address.length > (len * 2)) {
      return address.substring(0, len) + '...' + address.substring(address.length - len, address.length);
    }
    return address;
  }

}
